import BrowserService from '../../browser.service';
import { TesthubLibrariesConfig } from '../../config';
import { consoleLog, delay } from '../../utils';
const { title, pageUrl, dialogTitle, from, operation, testPage, screenshotName} = TesthubLibrariesConfig;
export default class TesthubAddLibrariesController extends BrowserService {
  constructor() {
    super();
  }
  async init() {
    await this.exportConfig(title, from);
    await this.page.goto(pageUrl, { waitUntil: 'networkidle' });
    await this.openAddDialog();
  }
  async openAddDialog() {
    await this.page.click(operation.selector);
    await delay(2000);
    const isVisible = await this.page.isVisible(`.dialog-title:has-text("${dialogTitle}")`);
    if(isVisible) {
      await this.addProject()
    } else {
      console.log('弹框加载失败')
    }
  }
  async addProject() {
    await this.submitForm(from);
    const fromDataState = await this.expectFormData(from);
    if (fromDataState) {
      await this.expectProject();
    }
  }
  async expectProject() {
    const isVisible = await this.page.isVisible(`.dialog-title:has-text("${dialogTitle}")`);
    const isNotify = await this.page.isVisible('.thy-notify-content');
    // 弹框消失证明添加成功
    if(isNotify && !isVisible) {
      const errText = await this.page.innerText('.thy-notify-content span');
      await consoleLog(`Testhub添加测试库测试 ${errText}`, this.logPath);
    } else {
      await this.page.goto(testPage.pageUrl, {
        waitUntil: 'networkidle',
      });
      await this.clickGroup(testPage.btnGroup);
      await this.expectTableTestResult(testPage.table);
    }
    await this.screenshot(screenshotName);
  }
}

/**
 * 创建测试库测试脚本
 * 1. 登录用户，保证用户处于登录状态[所有的页面测试都需要在登录状态下进行]
 * 2. 点击新建测试库按钮出现创建测试库弹框
 * 3. 输入表单内容
 * 4. 点击保存按钮
 * 5. 访问项目列表搜索项目
 * 6. 比对列表上的内容是否正确[选择按照时间排序]
 */
